<template>
  <div class="about">
    <div class="about-container">
      <Spin
        v-if="loading"
        tip="加载中..."
        style="display: block; margin: 40px auto"
      >
        <div style="height: 400px"></div>
      </Spin>

      <template v-else>
        <!-- 个人信息卡片 -->
        <div class="profile-card">
          <div class="avatar">
            <Avatar
              :size="120"
              :icon="userInfo.initials"
              :src="userInfo.avatar"
              :style="{ marginBottom: '16px' }"
            />
          </div>
          <h2 class="profile-name">{{ userInfo.name }}</h2>
          <p class="profile-title">{{ userInfo.title }}</p>
          <p class="profile-desc">{{ userInfo.description }}</p>
        </div>

        <!-- 内容部分 -->
        <div class="about-content">
          <!-- 关于我 -->
          <section class="section">
            <h3 class="section-title">关于我</h3>
            <div class="section-content" v-html="userInfo.aboutHtml"></div>
          </section>

          <!-- 联系我 -->
          <section class="section">
            <h3 class="section-title">联系我</h3>
            <div class="contact-info">
              <div
                v-for="contact in showContact"
                :key="contact.type"
                class="contact-item"
              >
                <span class="contact-label"
                  >{{ getContactLabel(contact.type) }}：</span
                >
                <a
                  :href="contact.href"
                  class="contact-value"
                  target="_blank"
                  rel="noopener noreferrer"
                  >{{ contact.value }}</a
                >
              </div>
            </div>
          </section>
        </div>
      </template>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from "vue";
import { Spin,Avatar } from "ant-design-vue";
import { useBlogStore } from "../stores/blog";

// 使用 Pinia store
const blogStore = useBlogStore();

// 个人信息数据
const userInfo = ref({
  name: "",
  title: "",
  avatar: "",
  description: "",
  initials: "GB",
  about: "",
  aboutHtml: "",
  skills: [],
  contact: [],
});
const showContact = computed(() => {
  return userInfo.value.contact.filter(contact => contact.visible)
})
const contactTypes = [
  { value: "phone", label: "手机", icon: "PhoneOutlined" },
  { value: "email", label: "邮箱", icon: "MailOutlined" },
  { value: "github", label: "GitHub", icon: "GithubOutlined" },
  { value: "wechat", label: "微信", icon: "WechatOutlined" },
  { value: "Weibo", label: "微博", icon: "WeiboCircleOutlined" },
  { value: "zhihu", label: "知乎", icon: "ZhihuOutlined" },
  { value: "qq", label: "QQ", icon: "QqOutlined" },
];
const getContactLabel = (type) => {
  const contactType = contactTypes.find((item) => item.value === type);
  return contactType?.label || type;
};
// 加载状态
const loading = ref(false);

// 获取个人信息
const fetchPersonalInfo = async () => {
  try {
    loading.value = true;
    const response = await blogStore.getPersonalInfo();
    userInfo.value = {
      ...response,
      avatar: import.meta.env.VITE_API_BASE_URL +response.avatar || "GB",
    };
  } catch (error) {
    console.error("获取个人信息失败:", error);
    // 设置默认值，防止页面白屏
    userInfo.value = {
      name: "Gulu Blog",
      title: "前端开发工程师",
      description: "专注于Vue.js生态系统开发，热衷于分享前端技术和最佳实践。",
      initials: "GB",
      about:
        "<p>你好！我是一名充满热情的前端开发工程师，拥有多年的Web开发经验。我专注于使用现代前端技术构建高质量、高性能的Web应用。</p><p>我热衷于学习新技术，并将其应用到实际项目中。同时，我也喜欢通过写作分享我的知识和经验，希望能够帮助到更多的开发者。</p><p>在这个博客中，我会分享关于前端开发的各种知识，包括Vue.js、React、TypeScript、性能优化等方面的内容。</p>",
      skills: [],
      contact: [],
      avatar: "",
    };
  } finally {
    loading.value = false;
  }
};

// 组件挂载时的处理
onMounted(async () => {
  await fetchPersonalInfo();
});
</script>

<style scoped>
.about {
  padding: 20px;
  background-color: #f0f2f5;
  min-height: calc(100vh - 64px); /* 减去Header的高度 */
}

.about-container {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 30px;
}

/* 个人信息卡片 */
.profile-card {
  background-color: #fff;
  border-radius: 8px;
  padding: 40px;
  text-align: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.avatar {
  margin-bottom: 20px;
}

.avatar-img {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  background-color: #1890ff;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto;
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
}

.avatar-text {
  color: white;
  font-size: 36px;
  font-weight: bold;
}

.profile-name {
  font-size: 24px;
  color: #262626;
  margin: 0 0 10px 0;
}

.profile-title {
  font-size: 16px;
  color: #666;
  margin: 0 0 15px 0;
}

.profile-desc {
  font-size: 14px;
  color: #999;
  line-height: 1.6;
  margin: 0;
  max-width: 600px;
  margin: 0 auto;
}

/* 内容部分 */
.about-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.section {
  background-color: #fff;
  border-radius: 8px;
  padding: 30px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.section-title {
  font-size: 20px;
  color: #262626;
  margin: 0 0 20px 0;
  padding-bottom: 10px;
  border-bottom: 2px solid #f0f0f0;
}

.section-content {
  color: #666;
  line-height: 1.8;
}

.section-content p {
  margin-bottom: 15px;
}

.section-content p:last-child {
  margin-bottom: 0;
}

/* 技能网格 */
.skills-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
}

.skill-item {
  padding: 15px;
  background-color: #fafafa;
  border-radius: 6px;
}

.skill-name {
  font-size: 16px;
  color: #262626;
  margin: 0 0 10px 0;
}

.skill-bar {
  height: 8px;
  background-color: #e8e8e8;
  border-radius: 4px;
  margin-bottom: 8px;
  overflow: hidden;
}

.skill-progress {
  height: 100%;
  background-color: #1890ff;
  transition: width 1s ease;
}

.skill-desc {
  font-size: 12px;
  color: #999;
  margin: 0;
}

/* 联系信息 */
.contact-info {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.contact-item {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}

.contact-label {
  font-weight: 500;
  color: #262626;
  margin-right: 10px;
}

.contact-value {
  color: #1890ff;
  text-decoration: none;
  transition: color 0.3s ease;
}

.contact-value:hover {
  color: #40a9ff;
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .about {
    padding: 10px;
  }

  .profile-card {
    padding: 20px;
  }

  .avatar-img {
    width: 100px;
    height: 100px;
  }

  .avatar-text {
    font-size: 30px;
  }

  .profile-name {
    font-size: 20px;
  }

  .section {
    padding: 20px;
  }

  .skills-grid {
    grid-template-columns: 1fr;
  }

  .contact-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 5px;
  }
}
</style>